﻿
using HuiZe.education.Common;
using HuiZe.education.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;

namespace HuiZe.education.ViewModel
{
    public class MessageViewModel : ViewModelBase
    {
        private int currentIndex;
        public int CurrentIndex
        {
            get { return currentIndex; }
            set
            {
                currentIndex = value;
                //ReqMessage(1);
            }
        }

        public ObservableCollection<MessageModel> Messages { get; set; }
        public MessageModel SelectedMessage { get; set; }
        public int pageCount { get; set; }
        public int limit { get; set; }

        private int _msgCount;
        public int msgCount
        {
            get { return _msgCount; }
            set
            {
                _msgCount = value;
                if (_msgCount <= limit)
                    pagemanagerVisibility = Visibility.Collapsed;
                else
                    pagemanagerVisibility = Visibility.Visible;
            }
        }

        public Visibility pagemanagerVisibility { get; set; }
        public MessageViewModel()
        {
            if (IsInDesignMode)
            {
                Messages = new ObservableCollection<MessageModel>()
                {
                   new MessageModel(){ title="账号到期",MsgTime=DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss"),content="账号还有7天到期"},
                   new MessageModel(){ title="账号到期",MsgTime=DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss"),content="账号还有7天到期"}
                };
                SelectedMessage = Messages[0];
            }
            Messages = new ObservableCollection<MessageModel>()
                {
                   new MessageModel(){ title="账号到期",MsgTime=DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss"),content="账号还有7天到期"},
                   new MessageModel(){ title="账号到期",MsgTime=DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss"),content="账号还有7天到期"}
                };
        }

        public void ReqMessage(int page)
        {
            //Dictionary<string, int> keyValuePairs = new Dictionary<string, int>();
            //keyValuePairs["limit"] = limit;
            //keyValuePairs["page"] = CurrentIndex;

            ReqPersonMessageModel msg = new ReqPersonMessageModel
            {
                limit = limit,
                page = page,
                token = HuiZeeducation.Instance.UserInfo.token,

            };


            var result = Request<ReqMessageModel>("/api/message/index", JsonHelper.ToJson(msg), "消息");
            if (result.Item1)
            {
                pageCount = result.Item2.data.last_page;
                Messages = new ObservableCollection<MessageModel>(result.Item2.data.data);
            }

        }

        public string GetMessage(int Id)
        {
            //Dictionary<string, int> keyValuePairs = new Dictionary<string, int>();
            //keyValuePairs["limit"] = limit;
            //keyValuePairs["page"] = CurrentIndex;

            ReqPersonMessageModel2 msg = new ReqPersonMessageModel2
            {
                id = Id,
                token = HuiZeeducation.Instance.UserInfo.token,

            };


            var result = Request<ReqMessageModel3>("/api/message/show", JsonHelper.ToJson(msg), "消息");
            if (result.Item1)
            {
                string msgContent = result.Item2.data.content;
                return msgContent;


            }
            else
            {
                return "";
            }

        }
    }
}
